// JavaScript Document
	function tableOrdering( order, dir, task ) {
		var form = document.adminForm;
	     
		form.order.value     = order;
		form.order_dir.value    = dir;
	
		document.adminForm.submit( task );
	}
	
	/**
	* Changes a dynamically generated list
	* @param string The name of the list to change
	* @param array A javascript array of list options in the form [key,value,text]
	* @param string The key to display
	* @param string The original key that was selected
	* @param string The original item value that was selected
	*/
	function changeDynaList( listname, source, key, orig_key, orig_val ) {
		var list = eval( 'document.adminForm.' + listname );
	
		// empty the list
		for (i in list.options.length) {
			list.options[i] = null;
		}
		i = 0;
		for (x in source) {
			if (source[x][0] == key) {
				opt = new Option();
				opt.value = source[x][1];
				opt.text = source[x][2];
	
				if ((orig_key == key && orig_val == opt.value) || i == 0) {
					opt.selected = true;
				}
				list.options[i++] = opt;
			}
		}
		list.length = i;
	}
	
	function displayDiv (divname, spanname, showtext, hidetext) { 
		var div = document.getElementById(divname);
		var span = document.getElementById(spanname);
		
	
		if (div.style.display == "none")	{
			div.style.display = "";
			span.innerHTML = hidetext;
		} else {
			div.style.display = "none";
			span.innerHTML = showtext;
		}
	}
	
	function switchDisplayDiv( prefix, newSuffix ){
		var newName = prefix + newSuffix;
		var currentSuffixDiv = document.getElementById('currentSuffix');
		var currentSuffix = currentSuffixDiv.innerHTML;	
		var oldName = prefix + currentSuffix;
		var newDiv = document.getElementById(newName);
		var oldDiv = document.getElementById(oldName);
	
		currentSuffixDiv.innerHTML = newSuffix;
		newDiv.style.display = "";
		oldDiv.style.display = "none";
	}
	
	function submitForm(form, task, id) {   
		// do field validation
			if (task == "validatefields") {
					if (!form.accept.checked) { 
						alert("You must accept our terms to submit this form.");
					}
					else if (form.question1.value == form.question2.value) { 
						alert("You must select two different questions.");
					}
					else if (form.answer1.value == form.answer2.value) { 
						alert("You must submit two different answers.");
					}
					else if (!form.answer1.value || !form.answer2.value) { 
						alert("You complete the entire form.");
					}
					else {
						form.task.value = task;
						form.id.value = id;
						form.submit();
					}
			} 
			else if (task == "processticket") {
					if (!form.subject.value || form.subject.value == " ") { 
						alert("Invalid Subject.");
					}
					else if (!form.description.value || form.description.value == " ") { 
						alert("Invalid Description.");
					}
					else {
						form.task.value = task;
						form.id.value = id;
						form.submit();
					}
			} 
			else {
				form.task.value = task;
				form.id.value = id;
				form.submit();
			}
	}

	/**
	 * 
	 * @param {Object} form
	 * @param {Object} task
	 * @param {Object} id
	 */
	function verifySubmitForm( form, task, id, url ) {
		
		// if url is present, do validation
		if (url) {		
			// loop through form elements and prepare an array of objects for passing to server
			var str = new Array();
			for(i=0; i<form.elements.length; i++)
			{
				postvar = {
					name : form.elements[i].name,
					value : form.elements[i].value,
					id : form.elements[i].id
				}
				str[i] = postvar;
			}
			// execute Ajax request to server
	        var a=new Ajax(url,{
	            method:"post",
				data:{"elements":Json.toString(str)},
	            onComplete: function(response){
	                var resp=Json.evaluate(response);
	                $("message-container").removeClass("ajax-loading").setHTML(resp.msg);
					if (resp.error != '1') {
						// if no error, submit form
						form.task.value = task;
						if (id) {
							form.id.value = id;
						}
						form.submit();
					}
	            }
	        }).request();
			
		}	
		else {
			form.task.value = task;
			if (id) {
				form.id.value = id;
			}
			form.submit();
		}
	}